<?php
/**
 * Created by PhpStorm.
 * User: Zhanghaiqiang
 * Date: 2020-03-12
 * Time: 00:18
 */

namespace App\Core\Endpoints\CandyBase;

use Carbon\Carbon;
use Endpoints\Base\Endpoint;
use Endpoints\Tools\UploadOssObject;
use Symfony\Component\Console\Input\InputArgument;

class UploadCandyStoryStrImage extends Endpoint
{
    const ARGUMENT_DATE       = 'date';
    const ARGUMENT_STR_IMAGE  = 'str_image';

    const FILE_PATH_PREFIX    = 'story';


    // 收参
    public function getArguments()
    {
        return [
            [static::ARGUMENT_DATE,InputArgument::REQUIRED,'日期',''],
            [static::ARGUMENT_STR_IMAGE,InputArgument::REQUIRED,'base64文件',''],
        ];
    }


    /**
     * @return mixed|string
     * @throws \Laravelladder\Core\Exceptions\Endpoints\ArgumentRequiredException
     * @throws \Laravelladder\Core\Exceptions\Validations\ValidationException
     */
    public function dryRun()
    {
        $saveDate   = $this->argument(static::ARGUMENT_DATE);
        $strFile    = $this->argument(static::ARGUMENT_STR_IMAGE);
        $dateObj    = new Carbon($saveDate);
        $saveYear   = $dateObj->year;
        $saveMonth  = ($dateObj->month < 10) ? '0'.$dateObj->month : $dateObj->month;
        $saveDay    = ($dateObj->day < 10) ? '0'.$dateObj->day : $dateObj->day;

        $fileName = time().rand(100,9999).rand(500,999). '.jpg';
        $filePath = "/tmp/".$fileName;
        file_put_contents($filePath,base64_decode($strFile));
        $objName     = UploadOssObject::getInstance()->setArguments([
            UploadOssObject::ARGUMENT_FILE_NAME  => $fileName,
            UploadOssObject::ARGUMENT_FILE_PATH  => $filePath,
            UploadOssObject::ARGUMENT_SAVE_YEAR  => $saveYear,
            UploadOssObject::ARGUMENT_SAVE_MONTH => $saveMonth,
            UploadOssObject::ARGUMENT_SAVE_DAY   => $saveDay,
            UploadOssObject::ARGUMENT_PREFIX     => static::FILE_PATH_PREFIX,
        ])->dryRun();
        unlink($filePath);
        return $objName;

    }
}